package org.nerve.hibernate.domain;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.io.Serializable;

/**
 * @project cloudOffice Entity.java
 * @datetime Jul 29, 2013 4:53:19 AM
 * @description 
 * 	基本实体   
 * @author 集成显卡 IBM 
 *			zxingming@qq.com
 *			zengxm@cn.ibm.com
 *
 * @change log:
 *
 */
public class BaseEntity implements Serializable {
	private static final long serialVersionUID = -6718838800112233445L;

	private Integer id;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	
	/**
	 * 判断一个对象是否已经持久化
	 * @method name: isUsing
	 * @return type: boolean
	 *	@return
	 */
	public boolean isUsing(){
		return id!=null && id>0;
	}

	@Override
	public int hashCode() {
		return id == null ? System.identityHashCode(this) : id.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass().getPackage() != obj.getClass().getPackage()) {
			return false;
		}
		final BaseEntity other = (BaseEntity) obj;
		if (id == null) {
			if (other.getId() != null) {
				return false;
			}
		} else if (!id.equals(other.getId())) {
			return false;
		}
		return true;
	}
	
	/**
	 * 只返回基本属性，List 这些不返回
	 * @method name: json
	 * @return type: String
	 *	@return
	 */
//	public String getJson(){
//		SerializeWriter writer = new SerializeWriter();
//		JSONSerializer out = new JSONSerializer(writer);
//		
//		out.config(SerializerFeature.DisableCircularReferenceDetect, true);
//		out.config(SerializerFeature.WriteMapNullValue, true);
//		
//		out.getValueFilters().add(new ValueFilter() {
//			public Object process(Object arg0, String arg1, Object arg2) {
//				if(arg2 instanceof List || arg2 instanceof Set){
//					return null;
//				}
//				
//				return arg2;
//			}
//		});
//		
//		out.write(this);
//		return out.toString();
//	}
}
